package o2o.web.actions.user;

import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyou.utils.security.EncrypMD5;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import o2o.business.sms.UserSMSInfoParams;
import o2o.cache.postman.CityPostmanMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.user.UserAddressMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserPwdMemory;
import o2o.config.dao.MongoDaoImpl;
import o2o.config.dao.UserDaoSql;
import o2o.config.dao.utils.Expression;
import o2o.config.dao.utils.QueryCondition;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.common.UserType;
import o2o.postman.model.CityPostman;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.user.UserAddress;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.user.UserAddressVO;
import o2o.system.Common;
import o2o.system.data.PROMPTS;
import o2o.system.data.SYS_SETTINGS;
import o2o.utils.MultilingualResult;
import o2o.web.actions.postman.PostmanMan;
import o2o.web.actions.shop.ShopMan;
import org.apache.commons.lang.StringUtils;

import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class User {
    private User() {
    }

    private static User single = new User();

    public static User getInstance() {
        if (single == null) {
            single = new User();
        }
        return single;
    }


    //修改用户类型
    public static boolean modifyUserType(String username, Integer userType) {
        if (null == userType || userType <= 0) {
            return false;
        }
        String sql = "update " + PojoTables.UserInfo + " set usertype = '" + userType + "' where username = '" + username + "' and delFlag = 0";
        try {
            UserDaoSql.getInstance().executeBySql(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

        UserInfoMemory.getInstance().modifyUserInfo(username);
        //同步修改用户类型
        JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_TYPE_MODIFY).setStr01(username).setInt01(userType);//o2o.business.jms  MQDataBusiness
        JmsSender.setTopicSender(notify);

        return true;
    }


    //添加用户 mod
    public static boolean addUser(String username, String phone, String password, int userType, String idCardNumber) throws Exception {
        if (ValidateUtil.isNull(username)) {
            throw new Exception("新增用户失败，用户名为空");
        }

        //验证是否已经存在当前帐号
        QueryCondition query = QueryCondition.getInstance();
        query.addCondition(Expression.getInstance().addChild("delFlag", 0).addChild("username", username));
        int count;
        try {
            count = UserDaoSql.getInstance().getCount(PojoTables.UserInfo, query);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new Exception("新增用户失败，请稍后再试.");
        }
        if (count > 0) {
            throw new Exception("新增用户失败，用户已存在");
        }

        //密码加密处理
        try {
            password = EncrypMD5.eccrypt(password);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("密码加密失败，请稍后再试");
        }

        // 直接注册用户
        String hxAccount = Common.hxName + username;
        String hxPwd;
        try {
            hxPwd = EncrypMD5.eccrypt(username);
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
            throw new Exception("环信密码加密失败，请稍后再试");
        }
        if (null == idCardNumber) {
            idCardNumber = "";
        }
        //注册用户信息
//        StringBuffer sbBuffer = new StringBuffer("insert into ")
//                .append(PojoTables.UserInfo)
//                .append("(userType,username,telPhone,hxAccount,hxPassword,createTime) values")
//                .append("(").append(userType).append(",'").append(username).append("','").append(username).append("','")
//                .append(hxAccount).append("','").append(hxPwd).append("',").append(DateUtils.getCurrTime()).append(")");
        StringBuffer sbBuffer = new StringBuffer("insert into ")
                .append(PojoTables.UserInfo)
                .append("(userType,username,phone,telPhone,hxAccount,hxPassword,createTime,idCardNumber) values")
                .append("(").append(userType).append(",'").append(username).append("'").append(",'").append(phone).append("','").append(phone).append("','")
                .append(hxAccount).append("','").append(hxPwd).append("',").append(DateUtils.getCurrTime()).append(",'").append(idCardNumber).append("')");

        //添加用户帐号信息
//        StringBuffer userPasql = new StringBuffer("INSERT INTO ").append(PojoTables.UserPasswordInfo).append("(username,password,createTime)values(")
//                .append("'").append(username).append("','").append(password).append("',").append(DateUtils.getCurrTime()).append(")");
        StringBuffer userPasql = new StringBuffer("INSERT INTO ").append(PojoTables.UserPasswordInfo).append("(username,password,createTime,hasSetPsw)values(")
                .append("'").append(username).append("','").append(password).append("',").append(DateUtils.getCurrTime()).append(",0)");
        Connection conn;
        try {
            conn = UserDaoSql.getInstance().getConnection();
        } catch (SQLException e1) {
            e1.printStackTrace();
            throw new Exception("新增用户失败，请稍后再试");
        }
        try {
            conn.setAutoCommit(false);
            UserDaoSql.getInstance().executeBySql(sbBuffer.toString(), conn);
            UserDaoSql.getInstance().executeBySql(userPasql.toString(), conn);

            conn.commit();
        } catch (Exception e) {
            UserDaoSql.getInstance().rollback(conn);
            e.printStackTrace();
            throw new Exception("新增用户失败，稍后再试");
        } finally {
            UserDaoSql.getInstance().close(conn);
        }

        //同步数据--增加帐号
        UserInfoMemory.getInstance().addUserInfo(username);    //添加新用户
        UserPwdMemory.getInstance().getPassword(username);    //添加新用户的密码缓存信息

        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_ADD).setStr01(username);//o2o.business.jms  MQDataBusiness
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }

        return true;
    }


    //添加用户 m,od
    public static boolean addUser(String username, String password, int userType) throws Exception {
        if (ValidateUtil.isNull(username)) {
            throw new Exception("新增用户失败，用户名为空");
        }

        //验证是否已经存在当前帐号
        QueryCondition query = QueryCondition.getInstance();
        query.addCondition(Expression.getInstance().addChild("delFlag", 0).addChild("username", username));
        int count;
        try {
            count = UserDaoSql.getInstance().getCount(PojoTables.UserInfo, query);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new Exception("新增用户失败，请稍后再试.");
        }
        if (count > 0) {
            throw new Exception("新增用户失败，用户已存在");
        }


        //密码加密处理
        try {
            password = EncrypMD5.eccrypt(password);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("密码加密失败，请稍后再试");
        }

        // 直接注册用户
        String hxAccount = Common.hxName + username;
        String hxPwd;
        try {
            hxPwd = EncrypMD5.eccrypt(username);
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
            throw new Exception("环信密码加密失败，请稍后再试");
        }
        //注册用户信息
        StringBuffer sbBuffer = new StringBuffer("insert into ")
                .append(PojoTables.UserInfo)
                .append("(userType,username,telPhone,hxAccount,hxPassword,createTime) values")
                .append("(").append(userType).append(",'").append(username).append("','").append(username).append("','")
                .append(hxAccount).append("','").append(hxPwd).append("',").append(DateUtils.getCurrTime()).append(")");
        //添加用户帐号信息
        StringBuffer userPasql = new StringBuffer("INSERT INTO ").append(PojoTables.UserPasswordInfo).append("(username,password,createTime,hasSetPsw)values(")
                .append("'").append(username).append("','").append(password).append("',").append(DateUtils.getCurrTime()).append(",0)");
        Connection conn;
        try {
            conn = UserDaoSql.getInstance().getConnection();
        } catch (SQLException e1) {
            e1.printStackTrace();
            throw new Exception("新增用户失败，请稍后再试");
        }
        try {
            conn.setAutoCommit(false);

            UserDaoSql.getInstance().executeBySql(sbBuffer.toString(), conn);
            UserDaoSql.getInstance().executeBySql(userPasql.toString(), conn);

            conn.commit();
        } catch (Exception e) {
            UserDaoSql.getInstance().rollback(conn);
            e.printStackTrace();
            throw new Exception("新增用户失败，稍后再试");
        } finally {
            UserDaoSql.getInstance().close(conn);
        }

        //同步数据--增加帐号
        UserInfoMemory.getInstance().addUserInfo(username);    //添加新用户
        UserPwdMemory.getInstance().getPassword(username);    //添加新用户的密码缓存信息
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_ADD).setStr01(username);//o2o.business.jms  MQDataBusiness
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }

        return true;
    }


    //添加用户
    public static ResultInfo addUser(UserInfo userInfo, String password) {
        if (null == userInfo) {
            return new ResultInfo("请完善信息");
        }

        String username = userInfo.getUsername();
        if (ValidateUtil.isNull(username)) {
            return new ResultInfo("用户账号不能为空");
        }

        //验证是否已经存在当前帐号
        QueryCondition query = QueryCondition.getInstance();
        query.addCondition(Expression.getInstance().addChild("delFlag", 0).addChild("username", username));
        int count = 0;
        try {
            count = UserDaoSql.getInstance().getCount(PojoTables.UserInfo, query);
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("新增用户失败，请稍后再试.");
        }
        if (count > 0) {
            return new ResultInfo("新增用户失败，用户已存在.");
        }


        //密码加密处理
        try {
            password = EncrypMD5.eccrypt(password);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultInfo("密码加密失败，请稍后再试");
        }

        // 直接注册用户
        String hxAccount = Common.hxName + username;
        String hxPwd;
        try {
            hxPwd = EncrypMD5.eccrypt(username);
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
            return new ResultInfo("环信密码加密失败，请稍后再试");
        }

        userInfo.setHxAccount(hxAccount);
        userInfo.setHxPassword(hxPwd);

        long time = DateUtils.getCurrTime();
        userInfo.setCreateTime(time);

        //添加用户帐号信息
        StringBuffer userPasql = new StringBuffer("INSERT INTO ").append(PojoTables.UserPasswordInfo).append("(username,password,createTime,hasSetPsw)values(")
                .append("'").append(username).append("','").append(password).append("',").append(time).append(",0)");
        Connection conn;
        try {
            conn = UserDaoSql.getInstance().getConnection();
        } catch (SQLException e1) {
            e1.printStackTrace();
            return new ResultInfo("新增用户失败，请稍后再试");
        }
        try {
            conn.setAutoCommit(false);

            UserDaoSql.getInstance().insertObj(PojoTables.UserInfo, userInfo);
            UserDaoSql.getInstance().executeBySql(userPasql.toString(), conn);

            conn.commit();
        } catch (Exception e) {
            UserDaoSql.getInstance().rollback(conn);
            e.printStackTrace();
            return new ResultInfo("新增用户失败，稍后再试.");
        } finally {
            UserDaoSql.getInstance().close(conn);
        }

        //同步数据--增加帐号
        UserInfoMemory.getInstance().addUserInfo(username);    //添加新用户
        UserPwdMemory.getInstance().getPassword(username);    //添加新用户的密码缓存信息
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_ADD).setStr01(username);//o2o.business.jms  MQDataBusiness
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }

        return new ResultInfo("", null);
    }

    //修改用户头像 姓名 性别
    public static ResultInfo modifyUserBaseInfo(String username, String name, String signPhoto, String sex, Integer userType) {
        if (ValidateUtil.isNull(username)) {
            return new ResultInfo("用户账号不能为空");
        }

        //修改信息
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.UserInfo).append(" set name = '").append(name).append("' ");
        if (ValidateUtil.isNotNull(signPhoto)) {
            sb.append(",signPhoto='").append(signPhoto).append("' ");
        }
        if (ValidateUtil.isNotNull(sex)) {
            sb.append(",sex='").append(sex).append("' ");
        }
        if (userType != null) {
            sb.append(",userType=").append(userType).append(" ");
        }
        sb.append(" where username ='").append(username).append("' and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("修改失败，请稍后再试");
        }

        UserInfoMemory.getInstance().modifyUserInfo(username);
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_INFO_MODIFY).setStr01(username);//o2o.business.jms  MQDataBusiness
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }

        return new ResultInfo();
    }

    //修改用户头像 姓名 性别 及照片
    public static ResultInfo modifyUserInfo(String username, String name, String signPhoto, String sex, String phone, String opUsername, String note) {
        if (ValidateUtil.isNull(username)) {
            return new ResultInfo("账号不能为空");
        }

        //修改信息
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.UserInfo).append(" set name = '").append(name).append("' ");
        if (ValidateUtil.isNotNull(signPhoto)) {
            sb.append(",signPhoto='").append(signPhoto).append("' ");
        }
        sb.append(",note='").append(note).append("' ");

        sb.append(",sex='").append(sex)
                .append("',telPhone='").append(phone).append("',updateUser='").append(opUsername).append("'  where username ='").append(username).append("' and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("修改失败，请稍后再试");
        }

        UserInfoMemory.getInstance().modifyUserInfo(username);
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_INFO_MODIFY).setStr01(username);//o2o.business.jms  MQDataBusiness
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }

        return new ResultInfo();
    }

    //修改用户头像 姓名 性别
    public static ResultInfo modifyUserInfo(String username, String name, String signPhoto, String sex, String phone, String opUsername) {
        if (ValidateUtil.isNull(username)) {
            return new ResultInfo("账号不能为空");
        }

        //修改信息
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.UserInfo).append(" set name = '").append(name).append("' ");
        if (ValidateUtil.isNotNull(signPhoto)) {
            sb.append(",signPhoto='").append(signPhoto).append("' ");
        }
        sb.append(",sex='").append(sex)
                .append("',telPhone='").append(phone).append("',updateUser='").append(opUsername).append("'  where username ='").append(username).append("' and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("修改失败，请稍后再试");
        }

        UserInfoMemory.getInstance().modifyUserInfo(username);
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_INFO_MODIFY).setStr01(username);//o2o.business.jms  MQDataBusiness
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }

        return new ResultInfo();
    }



    // 修改用户的身份证号
    public static boolean modifyUserIdCardNumber(String username, String idCardNumber, String opUsername) {
        if (null != idCardNumber) {
            StringBuffer sbID = new StringBuffer();
            sbID.append("UPDATE  ").append(PojoTables.UserInfo).append(" SET idCardNumber = '").append(idCardNumber).append("' WHERE username = '").append(username).append("' AND delFlag = 0");
            try {
                UserDaoSql.getInstance().executeUpdateBySql(sbID.toString());
            } catch (SQLException e) {
                e.printStackTrace();
            }
            UserInfoMemory.getInstance().modifyUserInfo(username);
            try {
                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_INFO_MODIFY).setStr01(username);
                JmsSender.setTopicSender(notify);

            } catch (Exception e) {
            }
        }
        return true;
    }

    //验证是否有修改权限
//    public static boolean hasShopAuth(int userType) {
//        //只有管理员、超级管理员、店主可以操作店铺
//        if (userType == UserType.SHOP_OWNER || userType == UserType.ADMIN || userType == UserType.SUPER_ADMIN) {
//            return true;
//        } else {
//            return false;
//        }
//    }

    public static boolean hasShopAuth(String username, int userType) {
        //只有管理员、超级管理员、店主可以操作店铺
        boolean isExactUserType = User.isExactUserType();
        if (isExactUserType) {
            if (userType == UserType.SHOP_OWNER || userType == UserType.ADMIN || userType == UserType.COMPANY || userType == UserType.SUPER_COMPANY || userType == UserType.SUPER_ADMIN) {
                return true;
            } else {
                ShopInfo shopInfo = ShopMemory.getInstance().getShopByUsername(username);
                if (shopInfo != null) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            if (userType == UserType.SHOP_OWNER || userType == UserType.ADMIN || userType == UserType.COMPANY || userType == UserType.SUPER_COMPANY || userType == UserType.SUPER_ADMIN) {
                return true;
            } else {
                return false;
            }
        }
    }

    //验证是否有管理后台权限
    public static boolean hasAdminAuth(int userType) {
        //只有管理员、超级管理员可以操作店铺
        if (userType == UserType.ADMIN || userType == UserType.SUPER_ADMIN || userType == UserType.COMPANY || userType == UserType.SUPER_COMPANY || 0 == userType - UserType.FINANCE || userType == UserType.CUSTOMER_SERVICE || userType == UserType.BUSINESS_MAN || userType == UserType.AGENT_MANAGER) {
            return true;
        } else {
            return false;
        }
    }

    //验证是否有管理后台权限
    public static boolean hasSuperAdminAuth(int userType) {
        //只有管理员、超级管理员可以操作店铺
        if (userType == UserType.SUPER_ADMIN || userType == UserType.SUPER_COMPANY || userType == UserType.COMPANY) {
            return true;
        } else {
            return false;
        }
    }

    //验证是否有管理后台权限
    public static boolean hasServiceAuth(int userType) {
        //只有管理员、超级管理员可以操作店铺
        if (userType == UserType.ADMIN || userType == UserType.SUPER_ADMIN || userType == UserType.SUPER_COMPANY || userType == UserType.COMPANY || userType == UserType.CUSTOMER_SERVICE || userType == UserType.AGENT_MANAGER) {
            return true;
        } else {
            return false;
        }
    }

    //验证是否有修改权限
    public static boolean isLocked(int isLock) {
        if (0 == 1 - isLock) {
            return true;
        } else {
            return false;
        }
    }

    //获取用户收货地址
    public static ResultInfo getUserAddress(String username) {
        ResultInfo info = new ResultInfo();
        List<UserAddress> list = UserAddressMemory.getInstance().listOfUser(username);
        if (null == list || list.size() <= 0) {
            info.setSuccess(true);
            info.setObj(null);
            return info;
        }
        List<UserAddressVO> voList = new ArrayList<UserAddressVO>();
        for (int i = 0; i < list.size(); i++) {
            UserAddressVO userAddressVO = new UserAddressVO();
            try {
                ObjectUtils.copyPropertiesExclude(list.get(i), userAddressVO, null);
            } catch (Exception e) {
                e.printStackTrace();
                info.setSuccess(false);
                info.setError("获取用户收货地址出错，请稍后再试");
            }
            voList.add(userAddressVO);
        }
        info.setSuccess(true);
        info.setObj(voList);
        return info;
    }


    //是否有该城市管理权限
    public static boolean hasCityAuth(String username, String city, int userType) {
        if (ValidateUtil.isNull(city)) {
            if (User.hasSuperAdminAuth(userType)) {
                //超级管理员
                return true;
            } else {
                return false;
            }
        }
        if (User.hasSuperAdminAuth(userType)) {
            //超级管理员
        } else if (User.hasAdminAuth(userType)) {            //普通管理员

        }
        return true;
    }


    public static boolean notHasCityAuth(String username, String city, int userType) {
        return !hasCityAuth(username, city, userType);
    }

    // 是否是实际的用户类型
    public static boolean isExactUserType() {
        if (SYS_SETTINGS.OPEN_EXACT_USER_TYPE != null && SYS_SETTINGS.OPEN_EXACT_USER_TYPE == 1) {
            return true;
        }
        return false;

    }


    // 是不是手机号登录
    public static boolean isPhoneLogin() {
        if (SYS_SETTINGS.LOGIN_TYPE_BY == null || SYS_SETTINGS.LOGIN_TYPE_BY == 0) {
            return false;
        } else {
            return true;
        }
    }

    // 获取用户信息
    public static UserInfo getUserInfoByUsername(String username) {
        if (isPhoneLogin()) {
            return UserInfoMemory.getInstance().getUserInfoByPhone(username);
        } else {
            return UserInfoMemory.getInstance().getUserInfo(username);
        }
    }

    // 生产一个用户的类
    static class UserNameCreate {
        private static long lastTimestamp = 0;
        private static int seq = 0;

        public static UserNameCreate getInstance() {
            return Singleton.INSTANCE.getInstance();
        }

        static enum Singleton {
            INSTANCE;

            private UserNameCreate single = null;

            private Singleton() {
                this.single = new UserNameCreate();
            }

            private UserNameCreate getInstance() {
                return this.single;
            }
        }

        private long timeGen() {
            long time = System.currentTimeMillis() / 1000;
            if (time == lastTimestamp) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return timeGen();
            } else {
                return time;
            }
        }

        public synchronized String nextCreateUsername() {
            long timestamp = DateUtils.getCurrTime() / 1000;
            if (this.lastTimestamp == timestamp) {
                this.seq++;
                if (seq > 9) {
                    seq = 0;
                    timestamp = this.timeGen();
                }
            } else {
                this.seq = 0;
            }

            if (timestamp < this.lastTimestamp) {
                try {
                    throw new Exception(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", this.lastTimestamp - timestamp));
                } catch (Exception var5) {
                    var5.printStackTrace();
                }
            }

            this.lastTimestamp = timestamp;
            String nextCreateUsername = timestamp + "" + seq;
            return nextCreateUsername;
        }
    }


    // 根据用户账号获取一个用户
    public static String getUniqueUserName(String username) {
        if (!User.isPhoneLogin()) {
            return username;
        } else if (StringUtils.isEmpty(username)) {
            username = UserNameCreate.getInstance().nextCreateUsername();
        }
        String name = username;
        username = null;
        for (int index = 0; index < 5; index++) {
            QueryCondition query = QueryCondition.getInstance();
            query.addCondition(Expression.getInstance().addChild("delFlag", 0).addChild("username", name));
            int count = 0;
            try {
                count = UserDaoSql.getInstance().getCount(PojoTables.UserInfo, query);
            } catch (SQLException e) {
                e.printStackTrace();
//
            }
            if (count > 0) {
                name = UserNameCreate.getInstance().nextCreateUsername();
            } else {
                username = name;
                break;
            }
        }
        return username;
    }

    /**
     * ----------- 注销
     */
    public static ResultInfo cancelAccount(Integer userType, String username, String telCode, String lang) {

        if (ValidateUtil.isNull(username)) {
            return new ResultInfo(MultilingualResult.getResultMsg(PROMPTS.ENTER_YOUR_MOBILE, lang));
        }
        if (ValidateUtil.isNull(telCode)) {
            return new ResultInfo(MultilingualResult.getResultMsg(PROMPTS.CHANGE_PASSWORD_CODE_EMPTY, lang));
        }
        UserInfo user = User.getUserInfoByUsername(username);
        if (user == null) {
            return new ResultInfo("当前用户未注册");
        }
        //验证是否已经存在当前帐号
        QueryCondition query = QueryCondition.getInstance();
        query.addCondition(Expression.getInstance().addChild("delFlag", 0).addChild("username", user.getUsername()));
        int count;
        try {
            count = UserDaoSql.getInstance().getCount(PojoTables.UserInfo, query);
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo(MultilingualResult.getResultMsg(PROMPTS.MODIFICATION_PASSWORD_FAILED, lang));
        }
        if (count <= 0) {
            return new ResultInfo(MultilingualResult.getResultMsg(PROMPTS.MOBILE_NOT_REGISTER, lang));
        }
        if (telCode == null || telCode.equals("")) {
            return new ResultInfo(MultilingualResult.getResultMsg(PROMPTS.GET_VERIFICATION_CODE, lang));
        } else {
            // 判断验证码是否正确
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("username", username);
            List<HashMap<String, Object>> list = MongoDaoImpl.getInstance()
                    .find(UserSMSInfoParams.MonUserSMSDB, UserSMSInfoParams.MonUserSMSCancelCol, map, 1);
            if (list == null || list.size() < 1) {
                return new ResultInfo(MultilingualResult.getResultMsg(PROMPTS.GET_VERIFICATION_CODE, lang));
            }
            // if (list.get(0).get("telCode") == null || list.get(0).get("telCode").toString().indexOf(telCode) < 0) {
            if (list.get(0).get("telCode") == null || !list.get(0).get("telCode").toString().contains("\"" + telCode + "\"")) {
                return new ResultInfo(MultilingualResult.getResultMsg(PROMPTS.ENTER_CORRECT_VERIFY_CODE, lang));
            }
        }


        // 商家或者骑手的话，注销后删除用户的商家或者骑手信息
        if (ValidateUtil.isAbsInteger(userType)) {
            if (null != user.getUserType() && !userType.equals(user.getUserType())) {
                return new ResultInfo("您还有其他身份信息，请先注销其他身份或联系客服");
            }
            switch (userType) {
                case UserType.SHOP_OWNER: {
                    // 删除店铺
                    // 根据账号获取店铺
                    ShopInfo shopInfo = ShopMemory.getInstance().getShopByUsername(username);
                    if (null != shopInfo) {
                        //删除店铺
                        ResultInfo resultInfo = ShopMan.deleteShop(shopInfo.getId() + "", user.getUsername(), user.getUserType());
                        if (null == resultInfo) {
                            return new ResultInfo("删除商家时失败，请稍后再试");
                        } else {
                            if (!resultInfo.isSuccess()) {
                                return new ResultInfo(resultInfo.getError());
                            }
                        }
                    }
                }
                break;
                case UserType.POSTMAN: {
                    // 删除骑手信息
                    ResultInfo resultInfo = PostmanMan.delPostman(username, null, username);
                    if (null == resultInfo) {
                        return new ResultInfo("删除骑手时失败，请稍后再试");
                    } else {
                        if (!resultInfo.isSuccess()) {
                            return new ResultInfo(resultInfo.getError());
                        }
                    }
                }
                break;
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.UserInfo).append(" set delFlag = 1,updateUser = '")
                .append(user.getUsername()).append("' where username = '").append(username).append("' and delFlag = 0");
        StringBuilder sbStr = new StringBuilder();
        sbStr.append("update ").append(PojoTables.UserPasswordInfo).append(" set delFlag = 1,updateUser = '")
                .append(user.getUsername()).append("' where username = '").append(username).append("' and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
            UserDaoSql.getInstance().executeBySql(sbStr.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("注销失败，请稍后再试");
        }
        UserInfoMemory.getInstance().del(username);
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_INFO_DEL).setStr01(username);//o2o.business.jms  MQDataBusiness
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }
        // 删除用户的密码
        UserPwdMemory.getInstance().delPassword(username);


        //删除门禁权限
//        if (ValidateUtil.isNotNull(username)) {
//            UserInfoExtra extra = UserInfoExtraMemory.getInstance().getByUsername(username);
//            if (null != extra && ValidateUtil.isNotNull(extra.getExtra1())) {
//                DeviceInfoMan.deleteUserDoorBindByUsername(username, user.getUsername());
//            }
//        }
        return new ResultInfo();
    }

    /**
     * ----------------
     */
    // 删除店铺或骑手时，判断是否有其他类型并修改成（多个用户）
    public static void delRevertUserStr(String usernames) {
        String[] userArr;
        if (usernames.indexOf(",") > -1) {
            userArr = usernames.split(",");
        } else {
            // 传入的是单个Id
            userArr = new String[1];
            userArr[0] = usernames;
        }
        if (null != userArr && userArr.length > 0) {
            for (int i = 0; i < userArr.length; i++) {
                String uName = userArr[i];
                if (ValidateUtil.isNull(uName)) {
                    continue;
                }
                delRevertUser(uName);
            }
        }
    }

    // 删除店铺或骑手时，判断是否有其他类型并修改成
    public static void delRevertUser(String username) {
        if (ValidateUtil.isNull(username)) {
            return;
        }


        ShopInfo shopInfo = ShopMemory.getInstance().getShopByUsername(username);
        if (null != shopInfo) {
            User.modifyUserType(username, UserType.SHOP_OWNER);
            return;
        }

        // 是否有骑手身份
        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(username);
        if (null != cityPostman) {
            // 将用户置为骑手
            User.modifyUserType(username, UserType.POSTMAN);
            return;
        }


        // 以上都没有  置为用户
        User.modifyUserType(username, UserType.USER);
    }

    // 删除商家
    public static void modifyUserLoginPhone(UserInfo user, String username, String phone) throws Exception {
        if (StringUtils.isBlank(phone)) {
            throw new Exception("登录账号不能为空");
        }
        if (phone.length() != 11) {
            throw new Exception("请输入11位的手机号");
        }
        UserInfo userInfo_db = UserInfoMemory.getInstance().getUserInfo(username);
        if (userInfo_db == null) {
            throw new Exception("未获取到用户信息");
        }
        if (!StringUtils.isBlank(userInfo_db.getPhone()) && userInfo_db.getPhone().equals(phone)) {
            UserInfoMemory.getInstance().modifyUserInfo(username);
        }
        userInfo_db = UserInfoMemory.getInstance().getUserInfo(username);
        if (!StringUtils.isBlank(userInfo_db.getPhone()) && userInfo_db.getPhone().equals(phone)) {
            return;
        }
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(PojoTables.UserInfo).append(" set updateUser ='").append(user.getUsername()).append("'")
                .append(",phone ='").append(phone.trim()).append("'").append(" where username ='").append(username).append("' and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("操作失败,请稍后重试");
        }
        //cache
        UserInfoMemory.getInstance().modifyUserInfo(username);
    }


    // 更新用户信息
    public static void modifyUserTriId(String username, Long triId) {
        if (null == triId || triId <= 0) {
            return;
        }
        String sql = "update " + PojoTables.UserInfo + " set triId = " + triId + " where username = '" + username + "' and delFlag = 0";
        try {
            UserDaoSql.getInstance().executeBySql(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        }

        UserInfoMemory.getInstance().modifyUserInfo(username);
        //同步修改用户
        JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_INFO_MODIFY).setStr01(username);
        JmsSender.setTopicSender(notify);
    }

    //----------------------------------通用方法
    //修改密码
    public static boolean modifyUserPassword(String username, String password) {
        try {
            String select = "select * from " + PojoTables.UserPasswordInfo + " where username = '" + username + "' and delFlag = 0";
            HashMap<String, Object> hashMap = null;
            hashMap = UserDaoSql.getInstance().queryMapBySql(select);
            if (hashMap == null) {
                StringBuffer userPasql = new StringBuffer("INSERT INTO ").append(PojoTables.UserPasswordInfo).append("(username,password,createTime,hasSetPsw)values(")
                        .append("'").append(username).append("','").append(password).append("',").append(DateUtils.getCurrTime()).append(",").append("0").append(")");
                UserDaoSql.getInstance().executeBySql(userPasql.toString());
                UserPwdMemory.getInstance().getPassword(username);    //添加新用户的密码缓存信息
            } else {
                String sql = "update " + PojoTables.UserPasswordInfo + " set password = '" + password + "',hasSetPsw=1 where username = '" + username + "' and delFlag = 0";
                UserDaoSql.getInstance().executeBySql(sql);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

//    public static void main(String[] args) {
//        for (int index = 0; index < 20; index++) {
//            String s = User.UserNameCreate.getInstance().nextCreateUsername();
//            System.out.println(s);
//        }
//    }
}
